package com.hn658.base.message.frontend.action.account.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.base.message.account.entity.AccountUserInfoEO;
import com.hn658.base.message.account.service.IAccountUserManagerService;
import com.hn658.base.message.frontend.action.account.vo.AccountQueryRequest;
import com.hn658.base.message.frontend.action.account.vo.AccountQueryResponse;
import com.hn658.base.message.frontend.action.account.vo.AccountVO;
import com.hn658.base.message.frontend.action.query.IMessageQueryAction;
import com.hn658.base.message.group.business.IGroupManager;
import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.shared.exception.BusinessException;


/**
 * 用户查询服务
 * @author ztjie
 *
 */
@Controller("accountQueryAction")
@Path("account")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")

public class AccountQueryAction extends AbstractBaseService implements IMessageQueryAction{
	
	@Autowired
	private IAccountUserManagerService accountUserService;
	
	@Autowired
	private IGroupManager groupManager;
	
	/**
	 * 通过用户ID，查询用户信息
	 * @param request
	 * @return
	 */
	@Path("queryAccount")
	@POST
	public IServiceResponse queryAccount(AccountQueryRequest request){
		AccountQueryResponse response=new AccountQueryResponse();
		try{
			AccountUserInfoEO account = accountUserService.findAccountByAppIdUid(request.getAppId(), request.getUserId());
			AccountVO accountVO = new AccountVO();
			if(account!=null){
				BeanUtils.copyProperties(account, accountVO);
				response.setAccount(accountVO);				
			}
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	/**
	 * 通过群ID，查询群内所有用户
	 * @param request
	 * @return
	 */
	@Path("queryGroupAccount")
	@POST
	public IServiceResponse queryGroupAccount(AccountQueryRequest request){
		AccountQueryResponse response=new AccountQueryResponse();
		try{
			List<String> groupUserIds = groupManager.queryGroupMemberIds(request.getAppId(), request.getGroupId());
			List<AccountUserInfoEO> accounts = accountUserService.queryAccountListByUid(request.getAppId(), groupUserIds);
			List<AccountVO> accountList = new ArrayList<AccountVO>();
			if(accounts!=null && accounts.size()>0){
				for(AccountUserInfoEO account : accounts){
					AccountVO accountVO = new AccountVO();
					BeanUtils.copyProperties(account, accountVO);
					accountList.add(accountVO);
				}
				response.setAccountList(accountList);
			}
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	/**
	 * 通过用户最后更新时间，查询更新过信息的用户
	 * @param request
	 * @return
	 */
	@Path("queryLastUpdateAccount")
	@POST
	public IServiceResponse queryLastUpdateAccount(AccountQueryRequest request){
		AccountQueryResponse response=new AccountQueryResponse();
		try{
			List<AccountUserInfoEO> accounts = accountUserService.queryAccountListByLastTime(request.getAppId(), request.getLastUpdateTime());
			List<AccountVO> accountList = new ArrayList<AccountVO>();
			if(accounts!=null && accounts.size()>0){
				for(AccountUserInfoEO account : accounts){
					AccountVO accountVO = new AccountVO();
					BeanUtils.copyProperties(account, accountVO);
					accountList.add(accountVO);
				}
				response.setAccountList(accountList);
				response.setLastUpdateTime(accounts.get(0).getLastUpdateTime());				
			}
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
